import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.formatters.DateFormatter;
import mx.utils.ObjectUtil;

import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.InventionVO;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private var _inventions:ArrayCollection;
public function set inventions(value:ArrayCollection):void
{
	_inventions	=	value;
	model.searchInventions	=	value;
	
	firms	=	new ArrayCollection();
	clients	=	new ArrayCollection();
	firms.addItem("");
	clients.addItem("");
	var invention:InventionVO;
	var firmName:String	=	"";
	var clientName:String=	"";
	for(var i:int=0; i<_inventions.length; i++)
	{
		invention	=	_inventions[i];
		firmName	=	invention.firmName;
		clientName	=	invention.clientName;
		
		if(!justIn(firms,firmName))	
		{
			firms.addItem(firmName);
		}
		if(!justIn(clients,clientName))
		{
			clients.addItem(clientName);
		}
	}
	
	showAllInventions();
}

private function justIn(arr:ArrayCollection,value:String):Boolean
{
	var	result:Boolean	=	false;
	for(var i:int=0; i<arr.length; i++)
	{
		if(arr[i] == value)
		{
			result	=	true;
			break;
		}
	}
	return result;
}

public function get inventions():ArrayCollection
{
	return _inventions;
}

public function showClientInventions(value:int):void
{
	switch(value)
	{
		case 0:
			showAllInventions();
			break;
		case 1:
			showInventions();
			break;
		case 2:
			showPatentApplications();
			break;
		case 3:
			showApplicationsInExams();
			break;
		case 4:
			showIssuedPatents();
			break;
		case 5:
			showAbandonments();
			break;
	}
}

private function showInventions():void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	for(var i:int=0; i<model.inventions.length; i++)
	{
		invention	=	model.inventions[i];
		if(invention.type == -1 && invention.status <6 )
		{
			clientInventions.addItem(invention);
		}
	}
	var sortField:SortField	=	new SortField();
	sortField.name			=	"value";
	sortField.descending	=	false;
	
	var sortField2:SortField	=	new SortField();
	sortField2.name			=	"lastActionDate";
	sortField2.descending	=	false;
	sortField2.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField2,sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	model.searchInventions	=	clientInventions;
}

private function showPatentApplications():void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	for(var i:int=0; i<model.inventions.length; i++)
	{
		invention	=	model.inventions[i];
		if(invention.type>-1 && invention.type<=1  && invention.status < 6)
		{
			clientInventions.addItem(invention);
		}
		else if(invention.type == -1 && invention.status == 6)
		{
			clientInventions.addItem(invention);
		}
	}
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"actionDueDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	
	model.searchInventions	=	clientInventions;
}

private function showApplicationsInExams():void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	for(var i:int=0; i<model.inventions.length; i++)
	{
		invention	=	model.inventions[i];
		if(invention.status > 6 && invention.status <9 && invention.ufDate != "" && invention.ufDate!="0000-00-00")
		{
			clientInventions.addItem(invention);
		}
		else if(invention.status == 6 && invention.type == 1)
		{
			clientInventions.addItem(invention);
		}
	}
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"actionDueDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	model.searchInventions	=	clientInventions;
}

private function showIssuedPatents():void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	for(var i:int=0; i<model.inventions.length; i++)
	{
		invention	=	model.inventions[i];
		if(invention.status == 9)
		{
			clientInventions.addItem(invention);
		}
	}
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"actionDueDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	model.searchInventions	=	clientInventions;
}

private function showAllInventions():void
{
	var sortField:SortField	=	new SortField();
	sortField.name			=	"actionDueDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	model.inventions.sort	=	sort;
	model.inventions.refresh();
	
	model.searchInventions	=	model.inventions;
	
}

private function showAbandonments():void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	for(var i:int=0; i<model.inventions.length; i++)
	{
		invention	=	model.inventions[i];
		if(invention.status == 10)
		{
			clientInventions.addItem(invention);
		}
	}
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"lastActionDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	model.searchInventions	=	clientInventions;
}

public function showFirmInventions(value:int):void
{
	switch(value)
	{
		case 0:
			showAllInventions();
			break;
		case 1:
			showPatentApplications();
			break;
		case 2:
			showApplicationsInExams();
			break;
		case 3:
			showIssuedPatents();
			break;
		case 4:
			showAbandonments();
			break;
	}
}
	

[Bindable]
private var firms:ArrayCollection;
[Bindable]
private var clients:ArrayCollection;

private function showUserInventions(user:String,event:Event):void
{
	var clientInventions:ArrayCollection	=	new ArrayCollection();
	var invention:InventionVO;
	
	if(user == "firm")
	{
		for(var i:int=0; i<model.inventions.length; i++)
		{
			invention	=	model.inventions[i];
			if(invention.status == 10)
			{
				clientInventions.addItem(invention);
			}
		}
	}
	else if(user == "client")
	{
		
	}
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"lastActionDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	clientInventions.sort	=	sort;
	clientInventions.refresh();
	model.searchInventions	=	clientInventions;
}

private function searchInventions(value:int):void
{
	model.searchInventions.filterFunction	=	filterInvention;
	
	var sortField:SortField	=	new SortField();
	sortField.name			=	"actionDueDate";
	sortField.descending	=	false;
	sortField.compareFunction=	compareDate;
	
	var sort:Sort			=	new Sort();
	sort.fields				=	[sortField];
	
	model.searchInventions.sort	=	sort;
	model.searchInventions.refresh();
}

private function compareDate(inventionOne:InventionVO,inventionTwo:InventionVO):int
{
	
	var dateFormat:DateFormatter	=	new DateFormatter();
	dateFormat.formatString			=	"YYYY/MM/DD";
	
	var date1:Number	=	Date.parse(dateFormat.format(inventionOne.actionDueDate));
	var date2:Number	=	Date.parse(dateFormat.format(inventionTwo.actionDueDate));
	//trace(inventionOne.actionDueDate+"  date1 "+inventionTwo.actionDueDate+"  date2 ");
	//trace(dateFormat.format(inventionOne.actionDueDate)+"  date1 "+dateFormat.format(inventionTwo.actionDueDate)+"  date2 ");
	trace(date1+"  date1 "+date2+"  date2 ");
	/*
	if(isNaN(date1))
	{
		date1	=	0;
	}
	if(isNaN(date2))
	{
		date2	=	0;
	}*/
	trace(date1+"  date1 "+date2+"  date2 ");
	return ObjectUtil.numericCompare(date1,date2);
}

private function filterInvention(item:Object):Boolean
{
	return filterClientName(item) && filterFirmName(item) && filterClientDocket(item) && filterFirmDocket(item) && filterSerialNumber(item);
}

private function filterClientDocket(item:Object):Boolean
{
	var clientDocketValue:String	=	clientDocket.text.toLowerCase();
	
	var clientDocketS:String=	item.docket.toLowerCase();
	
	return clientDocketS.indexOf(clientDocketValue)>=0;
}

private function filterFirmDocket(item:Object):Boolean
{
	var firmDocketValue:String	=	firmDocket.text.toLowerCase();
	
	var firmDocketS:String=	item.firmDocket.toLowerCase();
	
	return firmDocketS.indexOf(firmDocketValue)>=0;
}

private function filterSerialNumber(item:Object):Boolean
{
	var serialNumberValue:String	=	serialNumber.text.toLowerCase();
	
	var serialNumberS:String=	item.applicationNumber.toLowerCase();
	return serialNumberS.indexOf(serialNumberValue)>=0;
}

private function filterClientName(item:Object):Boolean
{
	if(selectClient == null)
	{
		return true;
	}
	
	var client:String	=	selectClient.selectedItem.toString();
	
	if(client == "")
	{
		return true;
	}
	var clientS:String	=	item.clientName;
	
	return clientS == client ? true:false;
}

private function filterFirmName(item:Object):Boolean
{
	if(selectFirm == null)
	{
		return true;
	}
	var firm:String	=	String(selectFirm.selectedItem);
	if(firm == "")
	{
		return true;
	}
	var firmS:String	=	item.firmName;
	trace((firmS == firm) +" firm");
	return firmS == firm ? true:false;
}